home *** CD-ROM | disk | FTP | other *** search
- /* Jonix 0.01 -- (c) Chris Rutter 1996
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include "jonix.h"
- #include "riscos.h"
-
- // #define DEBUG
- #undef stdout
-
- FILE *stdout;
-
- int main (int argc, char *argv[])
- {
- char *error;
- jonix_options options;
- FILE *fin, *fout;
- stdout = &(__iob[1]);
- if (paramexist ("--stdout", "-s", argc, argv) == 1)
- redirect (paramt ("--stdout", "-s", argc, argv));
- if (argc < 3) syntax ("Too few arguments.\n");
- fin = fopen (argv[1], "r");
- fout = fopen (argv[2], "w+");
- if (fin == 0)
- {
- fprintf (stdout, "Could not open input file.\n");
- if (fout != 0) fclose (fout);
- exit (0);
- }
- if (fout == 0)
- {
- fprintf (stdout, "Could not open output file.\n");
- if (fin != 0) fclose (fin);
- exit (0);
- }
- #ifdef DEBUG
- fprintf (stdout, "debug: %s->%s\n", argv[1], argv[2]);
- #endif
- options.width = 80;
- options.distance = 20;
- options.rowlength = 6;
- options.newline = 0;
- options.remove = 0;
- setparam (--width,-w,width);
- setparam (--rowlength,-r,rowlength);
- setparam (--distance,-d,distance);
- setparam (--newline,-n,newline);
- setparam (--remove,-v,remove);
- #ifdef riscos
- riscos_initialise ();
- #endif
- error = jonix (fin, fout, &options);
- if (error != 0) fprintf (stdout, error);
- fclose (fin);
- fclose (fout);
- #ifdef riscos
- riscos_shutdown ();
- #endif
- }
-
- char *jonix (FILE *fin, FILE *fout, jonix_options *options)
- {
- char *line, lastc;
- int newline, lines=0;
- line = malloc (MAX_LINE);
- #ifdef DEBUG
- fprintf (stdout, "debug: jonix () called. line at %d\n", *((int *) line));
- fprintf (stdout, "debug: options.width = %d\n", options->width);
- fprintf (stdout, "debug: options.distance = %d\n", options->distance);
- fprintf (stdout, "debug: options.rowlength = %d\n", options->rowlength);
- fprintf (stdout, "debug: options.newline = %d\n", options->newline);
- fprintf (stdout, "debug: options.remove = %d\n", options->remove);
- #endif
- while ((feof(fin) == 0) && (ferror(fin) == 0) && (ferror(fout) == 0))
- {
- newline = 0;
- fgets (line, MAX_LINE, fin);
- if (options->newline == 0) options->newline = line[strlen(line)-1];
- if (strlen(line) > 1) lastc = line[strlen(line)-2];
- else lastc = 0;
- if ((islower(line[0]) != 0) && (isalnum(line[0]) != 0) && (line[0] > 32) && (lastc > 0))
- {
- fseek (fout, -1L, SEEK_CUR);
- if ((fgetc(fout) == 10) || (fgetc(fout) == 13))
- {
- fseek (fout, -1L, SEEK_CUR);
- fputc (32, fout);
- }
- }
- if (lastc == 46) newline = -1;
- if (strlen(line) < (options->width - options->distance)) newline = -1;
- if (alphas(line) == 0) newline = -2;
- if (nonalphas(line) > options->rowlength) newline = -2;
- if (strlen(line) == 1) newline = -2;
- if (newline == -2 && lines > 0)
- {
- fseek (fout, -1L, SEEK_CUR);
- fputc (options->newline, fout);
- }
- if (newline == 0) line[strlen(line)-1] = 32;
- if (options->remove != 0)
- {
- if ((alphas(line) == 0) && (strlen(line) > 1))
- fputc (options->newline, fout);
- else fputs (line, fout);
- }
- else
- {
- fputs (line, fout);
- lines++;
- }
- #ifdef riscos
- riscos_poll (fin);
- #endif
- }
- return (0);
- }
-
- void syntax (char *message)
- {
- if (message != 0) fprintf (stdout, message);
- fprintf (stdout, "Syntax: jonix <input file> <output file> <options>\n");
- fprintf (stdout, "Options are --width,-w <width of text> (default=80)\n");
- fprintf (stdout, " --distance,-d <distance> (default=10)\n");
- fprintf (stdout, " --rowlength,-r <length> (default=6>\n");
- fprintf (stdout, " --newline,-n <newline character>\n");
- fprintf (stdout, " --remove,-v <0/1>\n");
- fprintf (stdout, " --stdout,-s <file to redirect output to>\n");
- exit (0);
- }
-
- int paramv (char *longs, char *shorts, int argc, char *argv[])
- {
- int p,r;
- for (p=0; p<argc; ++p)
- {
- if ((strcmp(argv[p],shorts) == 0) || (strcmp(argv[p],longs) == 0))
- {
- sscanf (argv[p+1], "%d", &r);
- }
- }
- return (r);
- }
-
- char *paramt (char *longs, char *shorts, int argc, char *argv[])
- {
- int p;
- char r[256];
- for (p=0; p<argc; ++p)
- {
- if ((strcmp(argv[p],shorts) == 0) || (strcmp(argv[p],longs) == 0))
- {
- sscanf (argv[p+1], "%s", r);
- }
- }
- return (r);
- }
-
- int paramexist (char *longs, char *shorts, int argc, char *argv[])
- {
- int p,e;
- for (p=0; p<argc; ++p)
- {
- if ((strcmp(argv[p],shorts) == 0) || (strcmp(argv[p],longs) == 0))
- {
- e = 1;
- }
- }
- return (e);
- }
-
- int nonalphas (char *text)
- {
- int p,nonalphas=0,longest=0;
- for (p=0; p<strlen(text); ++p)
- {
- if (isalnum(text[p]) == 0) nonalphas++;
- else
- {
- if (longest < nonalphas) longest = nonalphas;
- nonalphas = 0;
- }
- }
- if (longest < nonalphas) longest = nonalphas;
- return (longest);
- }
-
- int alphas (char *text)
- {
- int p, alphas=0;
- for (p=0; p<strlen(text); ++p)
- {
- if (isalnum(text[p]) != 0) alphas++;
- }
- return (alphas);
- }
-
- void redirect (char *file)
- {
- stdout = fopen (file, "w");
- if (stdout == 0) stdout = &__iob[1];
- }
-